<?php
/**
 * Defines Kodform_Validator_Size class
 *
 *
 * @copyright 	Copyright (c) 2006 Boris Tomic (kodmasin.net)
 * @category 	Kodform
 * @package 	Kodform
 * @author 		Boris Tomic
 * @license 	http://boris.kodmasin.net/kodform4:license The BSD License
 *
 */
 
require_once 'Kodform/Validator.php';

/**
 * check is value (usualy string) length in specified range 
 * 
 * */
class Kodform_Validator_Size extends Kodform_Validator{
	protected $maxSize;
	protected $minSize;
	/**
	 * creates Kodform_Validator_Size object
	 * 
	 * @param integer $maxSize upper limit for length. If it is set to 0 
	 * then it is ignored. length is valid if it is <= $maxSize
	 * @param integer $minSize lower limit for length. If it is set to 0 
	 * then it is ignored. length is valid if it is >= $maxSize
	 * @param string $error message which will be used if length is not in range
	 */
	public function __construct($maxSize, $minSize, $error){
		parent::__construct($error);
		$this->maxSize = $maxSize;
		$this->minSize = $minSize;
	}
	/*
	 * validation method
	 * 
	 * @see Kodform_Validator
	 */
	public function validate($value){
		$value = strlen($value);
		if($this->maxSize == 0 && $this->minSize == 0)
			return true;
		if($this->maxSize == 0){
			if($value >= $this->minSize)
				return true;
		}
		else if($this->minSize == 0){
			if($value <= $this->maxSize)
				return true;
		}
		else{
			if($value <= $this->maxSize && $value >= $this->minSize)
				return true;
		}
		return false;
	}
}
